package com.yeyaomai.dksns.flowis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.yeyaomai.dksns.flowis.dao.FlowAuthDao;
import com.yeyaomai.dksns.flowis.dao.FlowRoleDao;
import com.yeyaomai.dksns.flowis.domain.FlowAuth;
import com.yeyaomai.dksns.flowis.domain.FlowRole;
import com.yeyaomai.dksns.flowis.page.PageImplResult;
import com.yeyaomai.dksns.flowis.page.PageRequestOrder;
import com.yeyaomai.dksns.flowis.page.SplitConstant;
import com.yeyaomai.dksns.flowis.service.FlowRoleService;

@Service
public class FlowRoleServiceImpl implements FlowRoleService {

	@Resource
	private FlowRoleDao flowRoleDao;

	@Resource
	private FlowAuthDao flowAuthDao;

	@Override
	public void addFlowRole(FlowRole flowRole) {
		String authId = flowRole.getAuthId();
		String roleName = flowRole.getRoleName();
		String[] authList = authId.split(",");
		flowRoleDao.addFlowRole(flowRole);
		if (!authId.equals("")) {
			FlowRole role = flowRoleDao.getFlowRoleByRoleName(roleName);
			for (int i = 0; i < authList.length; i++) {
				FlowAuth flowAuth = new FlowAuth();
				flowAuth.setRoleId(role.getRoleId());
				flowAuth.setAuthId(Integer.valueOf(authList[i]));
				flowAuthDao.addFlowAuth_Role(flowAuth);
			}
		}

	}

	@Override
	public void updateFlowRole(FlowRole flowRole) {
		Long roleId = flowRole.getRoleId();
		flowRoleDao.updateFlowRole(flowRole);
		String authId = flowRole.getAuthId();
		if (!authId.equals("")) {
			String[] authList = authId.split(",");
			flowAuthDao.deleteFlowAuth_Role(roleId);
			if (authList.length > 0) {
				for (int i = 0; i < authList.length; i++) {
					FlowAuth flowAuth = new FlowAuth();
					flowAuth.setRoleId(roleId);
					flowAuth.setAuthId(Integer.valueOf(authList[i]));
					flowAuthDao.addFlowAuth_Role(flowAuth);
				}
			}
		}
	}

	@Override
	public void deleteFlowRole(Long roleId) {
		flowRoleDao.deleteRole_UserByRoleId(roleId);
		flowAuthDao.deleteFlowAuth_Role(roleId);
		flowRoleDao.deleteFlowRole(roleId);

	}

	@Override
	public FlowRole getFlowRoleByRoleId(Long roleId) {
		return flowRoleDao.getFlowRoleByRoleId(roleId);
	}

	@Override
	public boolean isExistFlowRole(String roleName) {
		FlowRole flowRole = new FlowRole();
		flowRole = flowRoleDao.getFlowRoleByRoleName(roleName);
		if (null == flowRole) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public Page<FlowRole> listFlowRole(Integer currentpage) {
		Integer flowUserCount = flowRoleDao.countFlowRole();
		PageRequestOrder request = new PageRequestOrder(currentpage,
				SplitConstant.PAGE_SIZE);
		List<FlowRole> flowRoleList = new ArrayList<FlowRole>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startNumber", request.start());
		map.put("endNumber", request.end());
		flowRoleList = flowRoleDao.listFlowRole(map);
		List<FlowAuth> flowAuthList = new ArrayList<FlowAuth>();
		FlowAuth flowAuth = new FlowAuth();
		for (int i = 0; i < flowRoleList.size(); i++) {
			flowAuthList = flowAuthDao.getFlowAuthByRoleId(flowRoleList.get(i)
					.getRoleId());
			if (flowAuthList.size() > 0) {
				for (int x = 0; x < flowAuthList.size(); x++) {
					flowAuth = flowAuthList.get(x);
					if (flowRoleList.get(i).getAuthId() == "") {
						flowRoleList.get(i).setAuthId("<span class='user_auth'>"+flowAuth.getAuthName()+"</span>");
					} else {
						flowRoleList.get(i).setAuthId(
								flowRoleList.get(i).getAuthId() + "<span class='user_auth'>"
										+ flowAuth.getAuthName()+"</span>");
					}

				}
			} else {
				flowRoleList.get(i).setAuthId("无");
			}
		}
		Page<FlowRole> result = new PageImplResult<FlowRole>(flowRoleList,
				request, flowUserCount);
		return result;
	}

	@Override
	public List<FlowRole> listFlowRoleByStatus() {

		return flowRoleDao.listFlowRoleByStatus();
	}

	public FlowRole getFlowRoleUserByUserId(Long userId) {
		
		return flowRoleDao.getFlowRoleUserByUserId(userId);
	}

}
